package cn.com.anypay.manager.controller.article;

import cn.com.anypay.manager.common.controller.BaseController;
import cn.com.anypay.manager.common.response.ApiResponse;
import cn.com.anypay.manager.model.Article;
import cn.com.anypay.manager.service.ArticleService;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章管理控制器
 */
@RestController
@RequestMapping("/api/article")
public class ArticleController extends BaseController {

    @Resource
    private ArticleService articleService;

    /**
     * 获取文章列表
     */
    @GetMapping("/list")
    public ApiResponse<List<Article>> list() {
        logger.info("获取文章列表");
        ResponseEntity<List<Article>> response = articleService.getArticleList();
        if (response.getStatusCode().is2xxSuccessful()) {
            return success(response.getBody());
        } else {
            return fail("获取文章列表失败");
        }
    }

    /**
     * 获取文章详情
     */
    @GetMapping("/{id}")
    public ApiResponse<Article> getInfo(@PathVariable Long id) {
        logger.info("获取文章详情, id={}", id);
        ResponseEntity<Article> response = articleService.getArticleInfo(id);
        if (response.getStatusCode().is2xxSuccessful()) {
            return success(response.getBody());
        } else {
            return fail("获取文章详情失败");
        }
    }

    /**
     * 下载文章
     */
    @GetMapping("/{id}/download")
    public ResponseEntity<String> download(@PathVariable Long id) {
        logger.info("下载文章, id={}", id);
        return articleService.downloadArticle(id);
    }

    /**
     * 删除文章
     */
    @DeleteMapping("/{id}")
    public ApiResponse<Void> remove(@PathVariable Long id) {
        logger.info("删除文章, id={}", id);
        ResponseEntity<Void> response = articleService.removeArticle(id);
        if (response.getStatusCode().is2xxSuccessful()) {
            return success();
        } else {
            return fail("删除文章失败");
        }
    }

    /**
     * 创建文章
     */
    @PostMapping
    public ApiResponse<Article> create(@RequestBody Article article) {
        logger.info("创建文章: {}", article);
        ResponseEntity<Article> response = articleService.createArticle(article);
        if (response.getStatusCode().is2xxSuccessful()) {
            return success(response.getBody());
        } else {
            return fail("创建文章失败");
        }
    }

    /**
     * 更新文章
     */
    @PutMapping("/{id}")
    public ApiResponse<Article> update(@PathVariable Long id, @RequestBody Article article) {
        logger.info("更新文章, id={}, article={}", id, article);
        ResponseEntity<Article> response = articleService.updateArticle(id, article);
        if (response.getStatusCode().is2xxSuccessful()) {
            return success(response.getBody());
        } else {
            return fail("更新文章失败");
        }
    }
}
